﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Newtonsoft.Json;
using System.Text;
using System.Net;
using System.IO;
using MM.Model;

namespace WebMMS
{
    public partial class test1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            #region
            //chargeInfo charge = new chargeInfo();
            //charge.money = 100;
            //charge.appid = 101;
            //charge.chargeid = 1;
            //charge.mode = 1;

            //simInfo sim = new simInfo();
            //sim.plmn = "46000";
            //sim.number = "15201877668";
            //sim.imsi = "460022018077374";
            //sim.imei = "358920045683155";

            //phoneInfo phone = new phoneInfo();
            //phone.position = 2;
            //phone.pid = 9999;
            //phone.mac = "18:87:96:B3:B1:43";
            //phone.height = 800;
            //phone.width = 480;
            //phone.name = "HTC Desire HD A9191";
            //phone.version = "2.3.5";

            //versionInfo version = new versionInfo();
            //version.code = 310;

            //infomation info = new infomation();
            //info.sim = sim;
            //info.phone = phone;
            //info.version = version;

            //JsonResultInfo result = new JsonResultInfo();
            //result.charge = charge;
            //result.info = info;

            //var json = JsonConvert.SerializeObject(result);

            //Response.Write(json);
            #endregion

            #region

            string sMessage = "{\"charge\":{\"money\":100,\"appid\":101,\"chargeid\":1,\"mode\":1},\"info\":{\"sim\":{\"plmn\":\"46000\",\"number\":\"15201877668\",\"imsi\":\"460022018077374\",\"imei\":\"358920045683155\"},\"phone\":{\"position\":2,\"pid\":9999,\"mac\":\"18:87:96:B3:B1:43\",\"height\":800,\"width\":480,\"name\":\"HTC Desire HD A9191\",\"version\":\"2.3.5\"},\"version\":{\"code\":310}}}";

            //string DsmpUrl = Request.Url.ToString().Replace("WebTest.aspx", "") + "charge3.aspx";

            string DsmpUrl = "http://localhost:2001/API.ashx";

            //把sMessage发送到指定的DsmpUrl地址上     
            Encoding encode = System.Text.Encoding.GetEncoding("gbk");
            byte[] arrB = encode.GetBytes(sMessage);
            HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create(DsmpUrl);
            myReq.Method = "POST";
            myReq.ContentType = "text/xml";
            myReq.ContentLength = arrB.Length;
            Stream outStream = myReq.GetRequestStream();
            outStream.Write(arrB, 0, arrB.Length);
            outStream.Close();


            //接收HTTP做出的响应
            WebResponse myResp = myReq.GetResponse();
            Stream ReceiveStream = myResp.GetResponseStream();
            StreamReader readStream = new StreamReader(ReceiveStream, encode);
            Char[] read = new Char[256];
            int count = readStream.Read(read, 0, 256);
            string str = null;
            while (count > 0)
            {
                str += new String(read, 0, count);
                count = readStream.Read(read, 0, 256);
            }
            readStream.Close();
            myResp.Close();

            Response.Write(str);

            #endregion
        }
    }
}